package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.config.GiteeImgBed;
import com.project.entity.Order1;
import com.project.entity.RestPOJO;
import com.project.entity.Stall;
import com.project.tools.GiteeUploadTools;
import com.project.tools.OSSTools;
import com.project.entity.Vehicle;
import com.project.service.StallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.project.service.impl.StallServiceImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="车位管理查询租用相关")
public class StallController {
    @Autowired
    private StallServiceImpl stallService;


    @ApiOperation(
            value = "根据用户ID和物业ID查找车位",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class

    )

//    @ApiImplicitParam(
////            @ApiImplicitParam(paramType="body",name="uid",value="用户ID",required=true),
////            @ApiImplicitParam(paramType="body",name="mid",value="物业ID",required=true)
//        name = "map",paramType ="body",examples =@Example({
//            @ExampleProperty(value="{'uid':'uid'}",mediaType = "application/json"),
//            @ExampleProperty(value="{'mid':'mid'}",mediaType = "application/json")
//    })
//    )
    @PostMapping("/getStall")
    public RestPOJO getStallbyManid( @RequestBody Map<String,Integer> map){
        RestPOJO restPOJO = new RestPOJO();

        List<Stall> list =  stallService.getStallByUidAndManId(map.get("uid"),map.get("mid"));
        if (list.size()>0){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(list);
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("查询失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "根据物业ID查找所有车位",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class

    )
    @GetMapping("/getStallsByManagementId/{id}")
    public RestPOJO getStallbyManid(@PathVariable("id") Integer mid){
        RestPOJO restPOJO = new RestPOJO();

        List<Stall> list =  stallService.getStallByManagementId(mid);
        if (list.size()>0){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(list);
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("查询失败");
        }
        return restPOJO;
    }

    @GetMapping("/zt_selStallAll")
    @ApiOperation(
            value = "查询未被租用的所有车位",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="query",name="currentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页数",required=true),
            @ApiImplicitParam(paramType="query",name="stall_size",value="车位大小",required=false),
            @ApiImplicitParam(paramType="query",name="stall_code",value="车牌号模糊查询",required=false),
    })
    public RestPOJO zt_selStallAll(Integer currentPage,Integer pageSize,String stall_size,String stall_code){
        RestPOJO rdt=new RestPOJO();
        PageInfo<Stall> stalls = stallService.zt_selStallAll(currentPage,pageSize,stall_size,stall_code);
        if(stalls!=null&&stalls.getList().size()>=0){
            rdt.setMessages("查询车位成功");
            rdt.setStatus(8848);
            rdt.setData(stalls);
            System.out.println(rdt.getData());

        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }
    @GetMapping("/zt_selStallById/{id}")
    @ApiOperation(
            value = "根据车位id查询车位",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="path",name="id",value="车位id",required=true),
    })
    public RestPOJO zt_selStallById(@PathVariable("id") Integer stall_id) {
        RestPOJO rdt=new RestPOJO();
        Stall stall = stallService.zt_selStallById(stall_id);
        if(stall!=null){
            rdt.setMessages("查询车位成功");
            rdt.setStatus(8848);
            rdt.setData(stall);
            System.out.println(rdt.getData());

        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }


    @ApiOperation(
            value = "修改车位信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PutMapping("/stall")
    public RestPOJO updateStall(@RequestBody @Validated Stall stall){

        System.out.println(stall.getStall_size());
        RestPOJO restPOJO = new RestPOJO();
        Integer count = stallService.updateStall(stall);
        if (count>0){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("更新成功");
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("更新失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "修改车位图片",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PutMapping("/img")
    public RestPOJO updateStallImg(Integer stall_id,MultipartFile stall_photo){
        GiteeUploadTools uploadTools = new GiteeUploadTools();
        RestPOJO restPOJO = new RestPOJO();
        String url="";
        try{
            url = uploadTools.uploadImg(stall_photo);
        } catch (Exception e) {
            url="";
        }
        if(url==null){
            restPOJO.setMessages("上传图片失败");
            return restPOJO;
        }

        Integer count = stallService.updateStallImg(stall_id,url);
        if (count>0){
            restPOJO.setData(url);
            restPOJO.setStatus(1000);
            restPOJO.setMessages("更新成功");
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("更新失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "查询所有车位信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @GetMapping ("/stalls")
    public RestPOJO getAllStall(){

        RestPOJO restPOJO = new RestPOJO();
        List<Stall> list = stallService.getAllStall();
        if (list.size()>0){
            restPOJO.setStatus(1000);
            restPOJO.setData(list);
            restPOJO.setMessages("查询成功");
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("查询失败");
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "添加车位",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/stall")
    public RestPOJO addStall(Stall stall, MultipartFile stallphoto){
        GiteeUploadTools tools = new GiteeUploadTools();
        String url ="";
        try {
            url =tools.uploadImg(stallphoto);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RestPOJO restPOJO = new RestPOJO();
       // String url= OSSTools.upLoad(System.currentTimeMillis()+"",stallphoto);
        stall.setPhoto(url);
        Integer count = stallService.addStall(stall);
        if (count>0){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("添加成功");
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("添加失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "用过用户ID查找车位",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @PostMapping("/getStallByUid")
    public RestPOJO getStallByUid(@RequestBody Map<String,Integer> map){

        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Stall> stallList= stallService.getStallByUid(map.get("uid"),map.get("currentPage"),map.get("pageSize"));
        if (stallList.getList().size()>0){
            restPOJO.setData(stallList);
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
        }else{
            restPOJO.setStatus(1001);
            restPOJO.setMessages("查询失败");
        }
        return restPOJO;
    }


    @GetMapping("/zt_selVehicle/{id}")
    @ApiOperation(
            value = "根据租客的uid查询车牌号,供用户定车位时选择",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            //如果是json，指定paramType为body
            @ApiImplicitParam(paramType="path",name="id",value="用户uid",required=true),
    })
    public RestPOJO zt_selVehicle(@PathVariable("id") Integer vehicle_uid) {
        RestPOJO rdt=new RestPOJO();
        List<Vehicle> list = stallService.zt_selVehicle(vehicle_uid);
        if(list!=null && list.size()>0){
            rdt.setMessages("查询车辆成功");
            rdt.setStatus(8848);
            rdt.setData(list);
            System.out.println(rdt.getData());

        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }
    @PutMapping("/zt_UpdateStallAndOrder")
    @ApiOperation(
            value = "根据租客id,车位id,车辆id，查询车牌号，修改车位状态，生成进行中订单,租借成功",
            response = RestPOJO.class
    )
    public RestPOJO zt_UpdateStallAndOrder(@RequestBody Order1 order1) {
        System.out.println(order1);
        RestPOJO rdt=new RestPOJO();
        Date date = new Date();
        order1.setOrder_startime(date);
        order1.setOrder_state("进行中");
        System.out.println(order1);
        int i = stallService.updateStall_stateAndOrder(order1);
        if(i == 2){
            rdt.setMessages("租借成功！");
            rdt.setStatus(8848);
        }
        else{
            rdt.setMessages("查询失败");
            rdt.setStatus(0000);
        }
        return rdt;
    }


}
